package example;

import java.net.*;
import java.io.*;

public class SimpleProtocol {
    private static final int CLIENT = 0;
    private static final int SERVER = 1;
    
    public String tableName = "";
    public String cmd = "";
    public static final String PARTICIPANT  = "PARTICIPANT";
    public static final String ROLLBACK  = "ROLLBACK";
    public static final String SELECT  = "SELECT";
    public static final String UPDATE  = "UPDATE";
    public static final String INSERT  = "INSERT";
    public static final String DELETE  = "DELETE";
    public static final String COMMIT  = "COMMIT";
    public static final String ABORT  = "ROLLBACK";    
    public static final String UNKOWN  = "UNKNOWN";
    private int side = CLIENT;
    
    
    public String senderIP = "";
    public String receiverIP = "";
    //return tableName
    public String queryType = "";
    public SimpleProtocol () {
    }
    public String joinType(String query){
    	String type = "";
    	return type;
    }
    public String setType(String query) {
    	queryType = "";
    	try {
	    	if(query.equals(SimpleProtocol.COMMIT)) {
	    		queryType = "COMMIT";
	    	}
	    	else {
		    	String temp = "";
		    	query = query.trim();
		    	int i = query.indexOf(" ");
		    	if (i>0)
		    		temp = query.substring(0, i);
		    	else  queryType = query;
		    	queryType = temp.trim();
	    	}
    	}
    	catch (Exception e) {
    		System.out.println("Exception set queryType");
    	}
    	return queryType;
    }
    public String setTableName(String query) {
    	query = query.trim();
    	query = query.toUpperCase();
    	setType(query);
    	String temp = "";
    	int i,j = 0;
    	if (queryType.toUpperCase().equals(UPDATE)) {
    		i = query.indexOf(" ");
    		j = query.indexOf(" SET ");
    		tableName = query.substring(i,j);    		
    	}
    	else if (queryType.toUpperCase().equals(INSERT)) {
    		i = query.indexOf(" INTO ");
    		j = query.indexOf(" VALUES ");
    		tableName = query.substring(i+6,j); 
    	}
    	else if (queryType.toUpperCase().equals(SELECT)){
    		i = query.indexOf(" FROM ");
    		j = query.indexOf(" WHERE ");    		
    		tableName = (j>0) ? query.substring(i+5,j) : query.substring(i+5,query.length());
    	}
    	else if (queryType.toUpperCase().equals(COMMIT)) {
    		tableName = this.tableName;
    	}
    	else if (queryType.toUpperCase().equals(ROLLBACK)) {
    		tableName = this.tableName;
    	}
    	tableName = tableName.trim();
    	tableName = tableName.toLowerCase();
    	return tableName;
    }
    public String processInput(String theInput) {
    	theInput = theInput.trim();
    	setType(theInput);
    	setTableName(theInput);
        return theInput;
    }
    public void getColList(String cmd) {
    	String content = "";
    	String[] colList;
    	int i,j = 0;
  		i = cmd.toUpperCase().indexOf(" SELECT ");
  		j = cmd.toUpperCase().indexOf(" FROM ");
  		content = cmd.substring(i+8,j);
  		content = content.trim();
  		System.out.println(content);
  		colList = content.split(",");
  		content = "";
    }
    public static void main(String args[]) {
    	String cmd = "select item from supplies";
    	SimpleProtocol kpp = new SimpleProtocol();
    	StringBuilder contents = new StringBuilder();
    	kpp.setTableName(cmd);
    }
}
